package light.head.dao.oa;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import light.head.constant.Parm;
import light.head.enm.RESULT;
import light.head.framework.base.BaseDAO;
import light.head.framework.entity.PageBean;
import light.head.framework.utils.DateUtils;
import light.head.model.Announcement;

import org.springframework.stereotype.Service;

@Service
public class AnnouncementDAO extends BaseDAO<Announcement, Integer>{
	
	public RESULT findByMap(List<Integer> ocs, Map<String, String> map, PageBean pb){
		StringBuffer hql = new StringBuffer("FROM Announcement a WHERE a.deleted = 0 AND a.oc IN(");
		List<Object> list = new ArrayList<Object>();
		for (Integer i : ocs) {
			hql.append("?,");
			list.add(i);
		}
		hql.deleteCharAt(hql.length() - 1).append(")");

		if (null != map) {
			String title = map.get(Parm.TITLE);
			String oc = map.get(Parm.OC);
			String start = map.get(Parm.ST);
			String end = map.get(Parm.END);
			String codes = map.get(Parm.CODES);
			String cuname = map.get(Parm.CUNAME);
			String uc = map.get(Parm.USER);
			String deleted = map.get(Parm.DELETED);
			if(isNotNull(title)) {
				hql.append(" AND a.title like ?");
				list.add("%" + title + "%");
			}if(isNotNull(oc)) {
				hql.append(" AND a.oc = ?");
				list.add(Integer.parseInt(oc));
			}if(isNotNull(start)) {
				hql.append(" AND a.rt >= ?");
				list.add(DateUtils.getDateByStr(start, "yyyy-MM-dd"));
			}if(isNotNull(end)) {
				hql.append(" AND a.rt <= ?");
				list.add(DateUtils.getDateByStr(end, "yyyy-MM-dd"));
			}if(isNotNull(codes)) {
				hql.append(" AND a.code in (?)");
				list.add(codes);				
			}if(isNotNull(cuname)) {
				hql.append(" AND a.cu in (SELECT u.code FROM UserInfo u WHERE u.realName like ?)");
				list.add("%" + cuname +"%");
			}if(isNotNull(uc)) {
				hql.append(" AND a.code in (SELECT annc.ac FROM AnnCollect annc WHERE annc.uc = ?)");
				list.add(Integer.parseInt(uc));
			}if(isNotNull(deleted)) {
				hql.append(" AND a.code in (SELECT ann.ac FROM AnnCollect ann WHERE ann.deleted = ?)");
				list.add(Boolean.parseBoolean(deleted));
			}
			hql.append(" ORDER BY a.code DESC");
		}
		
		if (findToPageBean(hql.toString(), pb, list) == RESULT.SUCCESS)
			return RESULT.SUCCESS;
		
		return RESULT.ERROR;
	}
		
		public RESULT findByMap1(List<Integer> ocs,Integer cu, Map<String, String> map, PageBean pb){
			StringBuffer hql = new StringBuffer("FROM Announcement a WHERE (a.status = 2 or a.cu = ?) AND a.oc IN(");
			List<Object> list = new ArrayList<Object>();
			list.add(cu);
			for (Integer i : ocs) {
				hql.append("?,");
				list.add(i);
			}
			hql.deleteCharAt(hql.length() - 1).append(")");
			if (null != map) {
				String title = map.get(Parm.TITLE);
				String oc = map.get(Parm.OC);
				String start = map.get(Parm.ST);
				String end = map.get(Parm.END);
				String codes = map.get(Parm.CODES);
				String cuname = map.get(Parm.CUNAME);
				String uc = map.get(Parm.USER);
				String deleted = map.get(Parm.DELETED);
				if(isNotNull(title)) {
					hql.append(" AND a.title like ?");
					list.add("%" + title + "%");
				}if(isNotNull(oc)) {
					hql.append(" AND a.oc = ?");
					list.add(Integer.parseInt(oc));
				}if(isNotNull(start)) {
					hql.append(" AND a.rt >= ?");
					list.add(DateUtils.getDateByStr(start, "yyyy-MM-dd"));
				}if(isNotNull(end)) {
					hql.append(" AND a.rt <= ?");
					list.add(DateUtils.getDateByStr(end, "yyyy-MM-dd"));
				}if(isNotNull(codes)) {
					hql.append(" AND a.code IN (?)");
					list.add(codes);				
				}if(isNotNull(cuname)) {
					hql.append(" AND a.cu in (SELECT u.code FROM UserInfo u WHERE u.realName like ?)");
					list.add("%" + cuname +"%");
				}if(isNotNull(uc)) {
					hql.append(" AND a.code in (SELECT annc.ac FROM AnnCollect annc WHERE annc.uc = ?)");
					list.add(Integer.parseInt(uc));
				}if(isNotNull(deleted)) {
					hql.append(" AND a.code in (SELECT ann.ac FROM AnnCollect ann WHERE ann.deleted = ?)");
					list.add(Boolean.parseBoolean(deleted));
				}
				hql.append(" ORDER BY a.code DESC");
			}
			
			if (findToPageBean(hql.toString(), pb, list) == RESULT.SUCCESS)
				return RESULT.SUCCESS;
			
			return RESULT.ERROR;
		}
	
	public RESULT update(Announcement ann) {
		if (null == ann || null == ann.getCode())
			return RESULT.PARAMETER_ERROR;
		
		Announcement old = get(ann.getCode());
		if (null == old)
			return RESULT.NOT_FIND;
		
		if(isNotNull(ann.getContent())) {
			old.setContent(ann.getContent());
		}if(isNotNull(ann.getTitle())) {
			old.setTitle(ann.getTitle());
		}if(null != ann.getRt()) {
			old.setRt(ann.getRt());
		}if(null != ann.getDeleted()){
			old.setDeleted(ann.getDeleted());
		}if(null != ann.getDraft()) {
			old.setDraft(ann.getDraft());
		}

		RESULT result = upd(old);
		return result;
	}

	
	/**
	 * 根据机构编号查询公告
	 * @param	机构编号
	 * @param	获取记录总数
	 */
	public List<Announcement> findOc(List<Integer> ocs, Integer limit){
		if (null == ocs)return null;
		List<Object> list = new ArrayList<Object>();
		StringBuffer hql = new StringBuffer("FROM Announcement WHERE status = 2 AND deleted = 0 AND oc IN(");
		for (Integer i : ocs) {
			hql.append("?,");
			list.add(i);
		}
		hql.deleteCharAt(hql.length() - 1).append(") ORDER BY rt DESC");
		return find(hql.toString(), list.toArray());
	}
}


